package com.hysf.ioc.pro4_ioc_factorybean;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
public class FruitFactoryTest {
	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(FruitFactoryTest.class);
		Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
		
		Fruit fruit = context.getBean(Fruit.class);
		Fruit fruit1 = (Fruit) context.getBean("fruitFactoryBean");
		System.out.println(fruit == fruit1);
		
//		Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);

//		Fruit fruit1 = (Fruit) context.getBean("fruitFactoryBean");
//		System.out.println(fruit == fruit1);
//
//		FruitFactoryBean fruitFactoryBean = context.getBean(FruitFactoryBean.class);
//		System.out.println(fruitFactoryBean);
		
		FruitFactoryBean fruitFactoryBean = (FruitFactoryBean) context.getBean("&fruitFactoryBean");
//		System.out.println(fruitFactoryBean);
		System.out.println("===================================================================================");
		ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) context.getAutowireCapableBeanFactory();
		DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) beanFactory;
		// 获取所有单例 Bean 的名称
		String[] singletonNames = registry.getSingletonNames();
		
		System.out.println("单例 Bean 数量: " + singletonNames.length);
		for (String name : singletonNames) {
			System.out.println("Bean 名称: " + name + ", 类型: " + registry.getSingleton(name).getClass().getName());
		}
	}
	@Bean
	public FruitFactoryBean fruitFactoryBean() {
		return new FruitFactoryBean();
	}
}
